﻿namespace Novel.Server.Middleware
{
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Http;
    using Novel.Server.Response;
    using System.Text.Json;
    using System.Threading.Tasks;

    public class CustomCookieAuthenticationEvents : CookieAuthenticationEvents
    {
        public override Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context)
        {
            context.Response.StatusCode = StatusCodes.Status401Unauthorized;
            context.Response.ContentType = "application/json";
            var result = JsonSerializer.Serialize(ApiResponse.ErrorResponse("Unauthorized", 401));
            return context.Response.WriteAsync(result);
        }

        public override Task RedirectToAccessDenied(RedirectContext<CookieAuthenticationOptions> context)
        {
            context.Response.StatusCode = StatusCodes.Status403Forbidden;
            context.Response.ContentType = "application/json";
            var result = JsonSerializer.Serialize(ApiResponse.ErrorResponse("Access Denied", 403));
            return context.Response.WriteAsync(result);
        }
    }

}
